package com.healthtop.utils;

import lombok.Data;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * * 椿熙堂信息研发部
 * Author: 道长 on 2016/10/13.
 */
@Component
public class ImageUtils {

        private Font definedFont = getDefinedFont();

        public ImageCoverFile createImageCoverFile(){
            return new ImageCoverFile();
        }
        @Data
        public class ImageCoverFile{

            private InputStream template;

            private InputStream avatarFile;

            private InputStream qrcodeFile;

            private String name;

            private String memberCardNo;

            private ByteArrayOutputStream outputFile;

            private String serviceTelephone;

            private String dept;

            private String job;

            private String english;

        }
        public void drawEmpCard(ImageCoverFile imageCoverFile){
            try {
                BufferedImage originImage = ImageIO.read(imageCoverFile.getTemplate());
                BufferedImage avatar=ImageIO.read(imageCoverFile.getAvatarFile());
                int width = originImage.getWidth();
                int height = originImage.getHeight();
                BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
                Graphics2D g2d = (Graphics2D) image.getGraphics();
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.drawImage(originImage, 0, 0, width, height, null);
                g2d.drawImage(avatar, 123, 212, 160, 215, null);
//                ImageIO.write(image, "PNG", imageCoverFile.getOutputFile());
//                Font font = Font.createFont(Font.TRUETYPE_FONT,ImageUtils.class.getResourceAsStream("/font/msyh.ttf"));
//                font.deriveFont(30);
//                font.deriveFont(Font.BOLD);
                g2d.setFont(definedFont);
                g2d.setPaint(new Color(0, 0, 0));
                g2d.setFont(new Font("微软雅黑", Font.BOLD, 30));
                g2d.drawString(imageCoverFile.getName(), 160, 467);
                g2d.setFont(new Font("微软雅黑", Font.BOLD, 30));
                g2d.drawString(imageCoverFile.getDept(), 160, 507);
                g2d.setFont(new Font("微软雅黑", Font.BOLD, 30));
                g2d.drawString(imageCoverFile.getJob(), 170, 547);
                //ImageIO.write(image, "PNG",new FileOutputStream("e:/a.png"));
                ImageIO.write(image, "PNG", imageCoverFile.getOutputFile());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    public Font getDefinedFont() {
        if (ObjectUtils.isEmpty(definedFont)) {
            InputStream is = null;
            BufferedInputStream bis = null;
            try {
                is = ImageUtils.class.getResourceAsStream("/font/msyh.ttf");
                bis = new BufferedInputStream(is);
                // createFont返回一个使用指定字体类型和输入数据的新 Font。<br>
                // 新 Font磅值为 1，样式为 PLAIN,注意 此方法不会关闭 InputStream
                definedFont = Font.createFont(Font.TRUETYPE_FONT, bis);
                // 复制此 Font对象并应用新样式，创建一个指定磅值的新 Font对象。
                definedFont = definedFont.deriveFont(30);
            } catch (FontFormatException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (null != bis) {
                        bis.close();
                    }
                    if (null != is) {
                        is.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return definedFont;
    }
    /**
         * text : 长度 2-6
         * font :
         ＊ imagefile : 输出文件
         */
        public void generate(ImageCoverFile imageCoverFile) {
            try {
                BufferedImage originImage = ImageIO.read(imageCoverFile.getTemplate());
                BufferedImage avatar = ImageIO.read(imageCoverFile.getAvatarFile());
                int width = originImage.getWidth();
                int height = originImage.getHeight();
                BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
                Graphics2D g2d = (Graphics2D) image.getGraphics();
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.drawImage(originImage, 0, 0, width, height, null);
                if(avatar.getWidth() ==avatar.getHeight()){
                    g2d.drawImage(avatar, 56, 138, 182, 182, null);
                }else {
                    g2d.drawImage(avatar, 56, 118, 182, 223, null);
                }
                g2d.setFont(new Font("微软雅黑", Font.BOLD, 35));
                g2d.setPaint(new Color(0, 51, 153));
                g2d.drawString(imageCoverFile.getName(), 480, 240);
                g2d.setFont(new Font("微软雅黑", Font.BOLD, 35));
                g2d.setPaint(new Color(0, 51, 153));
                g2d.drawString(imageCoverFile.getMemberCardNo(), 480, 320);
                ImageIO.write(image, "PNG", imageCoverFile.getOutputFile());

//                 竖着的学生/教师证
//                g2d.drawImage(avatar, 118, 180, 230, 230, null);
//                g2d.setFont(new Font("微软雅黑", Font.BOLD, 40));
//                g2d.setPaint(new Color(8, 19, 3));
//                g2d.drawString(imageCoverFile.getServiceTelephone(), 170, 80);
//                g2d.setFont(new Font("微软雅黑", Font.BOLD, 30));
//                g2d.drawString(imageCoverFile.getEnglish(), 125, 120);
//                g2d.setFont(new Font("微软雅黑", Font.BOLD, 25));
//                g2d.drawString(imageCoverFile.getName(), 100, 490);
//                g2d.setFont(new Font("微软雅黑", Font.BOLD, 25));
//                g2d.drawString(imageCoverFile.getMemberCardNo(), 100, 540);
//                ImageIO.write(image, "PNG", imageCoverFile.getOutputFile());
            }catch (Exception exception){
                exception.printStackTrace();
            }
        }
}
